package com.iplant.basic.controller.bms;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.BMSService;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.bms.BMSTeam;
import com.iplant.basic.service.po.bms.BMSTeamItem;
import com.iplant.basic.service.po.bms.BMSWorkType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/TeamManage")
public class TeamManageController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(TeamManageController.class);
	@Autowired
	BMSService wBMSService;

	public TeamManageController() {
		// TODO Auto-generated constructor stub
	}

	@GetMapping("/All")
	Object All(HttpServletRequest request) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wLoginUser = GetSession(request);
			String wName = StringUtils.parseString(request.getParameter("Name"));
			int wFactoryID = StringUtils.parseInt(request.getParameter("FactoryID"),-1);

			int wWorkshopID = StringUtils.parseInt(request.getParameter("WorkshopID"),-1);
			int wDepartmentID = StringUtils.parseInt(request.getParameter("DepartmentID"),-1);
			int wModuleID = StringUtils.parseInt(request.getParameter("ModuleID"),-1);
			int wLeaderID = StringUtils.parseInt(request.getParameter("LeaderID"),-1);
			int wActive = StringUtils.parseInt(request.getParameter("Active"),-1);

			Pagination wPagination = GetPagination(request);

			ServiceResult<List<BMSTeam>> wServerRst = wBMSService.BMS_GetTeamList(wLoginUser, wName, wFactoryID,
					wWorkshopID, wDepartmentID, wModuleID, wLeaderID, wActive, wPagination);

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}


	@GetMapping("/Info")
	Object Info(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);

			int wID = StringUtils.parseInt(request.getParameter("ID"));
			String wCode = StringUtils.parseString(request.getParameter("Code"));

			ServiceResult<BMSTeam> wServiceResult = wBMSService.BMS_GetTeam(wLoginUser, wID, wCode);


			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServiceResult.Result);
			}

		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}


	@PostMapping("/Update")
	Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);
			int wUserID = wBMSEmployee.getID();
			String wUserName = wBMSEmployee.getName();

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			BMSTeam wBMSTeam = CloneTool.Clone(wParam.get("data"), BMSTeam.class);

			if (wBMSTeam.ID <= 0) {
				wBMSTeam.CreatorID = wUserID;
				wBMSTeam.CreatorName = wUserName;
				wBMSTeam.CreateTime = Calendar.getInstance();
			}
			wBMSTeam.EditorID = wUserID;
			wBMSTeam.EditorName = wUserName;
			wBMSTeam.EditTime = Calendar.getInstance();

			ServiceResult<Integer> wServerRst = wBMSService.BMS_UpdateTeam(wBMSEmployee, wBMSTeam);

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {

				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBMSTeam);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, wBMSTeam);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/Delete")
	Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			BMSTeam wBMSTeam = CloneTool.Clone(wParam.get("data"), BMSTeam.class);

			ServiceResult<Integer> wServerRst = wBMSService.BMS_DeleteTeam(wBMSEmployee, wBMSTeam);

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {

				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBMSTeam);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, wBMSTeam);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/Active")
	Object Active(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data") || !wParam.containsKey("Active")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}
			int wActive = StringUtils.parseInt(wParam.get("Active"));
			List<BMSTeam> wBMSTeamList = CloneTool.CloneArray(wParam.get("data"), BMSTeam.class);

			ServiceResult<Integer> wServerRst = new ServiceResult<Integer>(0);
			for (BMSTeam wBMSTeam : wBMSTeamList) {
				wServerRst = wBMSService.BMS_ActiveTeam(wBMSEmployee, wBMSTeam, wActive);
				if (StringUtils.isNotEmpty(wServerRst.getFaultCode())) {
					break;
				}
			}

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/ItemAll")
	Object ItemAll(HttpServletRequest request) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wLoginUser = GetSession(request);
			String wName = StringUtils.parseString(request.getParameter("Name"));
			int wFactoryID = StringUtils.parseInt(request.getParameter("FactoryID"),-1);
			int wWorkTypeID = StringUtils.parseInt(request.getParameter("WorkTypeID"),-1);
			int wTeamID = StringUtils.parseInt(request.getParameter("TeamID"),-1);

			int wWorkshopID = StringUtils.parseInt(request.getParameter("WorkshopID"),-1);
			int wWorkAreaID = StringUtils.parseInt(request.getParameter("WorkAreaID"),-1);
			int wStationID = StringUtils.parseInt(request.getParameter("StationID"),-1);
			String wStationCode = StringUtils.parseString(request.getParameter("StationCode"));
			int wActive = StringUtils.parseInt(request.getParameter("Active"),-1);

			Pagination wPagination = GetPagination(request);

			ServiceResult<List<BMSTeamItem>> wServerRst = wBMSService.BMS_GetTeamItemList(wLoginUser, wName, wFactoryID,wWorkTypeID,
					wWorkshopID, wTeamID, wWorkAreaID, wStationID,wStationCode, wActive, wPagination);

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/ItemUpdate")
	Object ItemUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);
			int wUserID = wBMSEmployee.getID();
			String wUserName = wBMSEmployee.getName();

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			BMSTeamItem wBMSTeam = CloneTool.Clone(wParam.get("data"), BMSTeamItem.class);

			if (wBMSTeam.ID <= 0) {
				wBMSTeam.CreatorID = wUserID;
				wBMSTeam.CreatorName = wUserName;
				wBMSTeam.CreateTime = Calendar.getInstance();
			}
			wBMSTeam.EditorID = wUserID;
			wBMSTeam.EditorName = wUserName;
			wBMSTeam.EditTime = Calendar.getInstance();

			ServiceResult<Integer> wServerRst = wBMSService.BMS_UpdateTeamItem(wBMSEmployee, wBMSTeam);

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {

				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBMSTeam);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, wBMSTeam);
			}
		} catch (Exception ex) { 
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/ItemDelete")
	Object ItemDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			BMSTeamItem wBMSTeamItem = CloneTool.Clone(wParam.get("data"), BMSTeamItem.class);

			ServiceResult<Integer> wServerRst = wBMSService.BMS_DeleteTeamItem(wBMSEmployee, wBMSTeamItem);

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {

				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBMSTeamItem);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, wBMSTeamItem);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/ItemActive")
	Object ItemActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data") || !wParam.containsKey("Active")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}
			int wActive = StringUtils.parseInt(wParam.get("Active"));
			List<BMSTeamItem> wBMSTeamList = CloneTool.CloneArray(wParam.get("data"), BMSTeamItem.class);

			ServiceResult<Integer> wServerRst = new ServiceResult<Integer>(0);
			for (BMSTeamItem wBMSTeamItem : wBMSTeamList) {
				wServerRst = wBMSService.BMS_ActiveTeamItem(wBMSEmployee, wBMSTeamItem, wActive);
				if (StringUtils.isNotEmpty(wServerRst.getFaultCode())) {
					break;
				}
			}

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/WorkTypeAll")
	Object WorkTypeAll(HttpServletRequest request) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wLoginUser = GetSession(request);
			String wName = StringUtils.parseString(request.getParameter("Name")); 
			int wActive = StringUtils.parseInt(request.getParameter("Active"),-1);

			Pagination wPagination = GetPagination(request);

			ServiceResult<List<BMSWorkType>> wServerRst = wBMSService.BMS_QueryWorkTypeList(wLoginUser, wName, wActive, wPagination);

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/WorkTypeUpdate")
	Object WorkTypeUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);
			int wUserID = wBMSEmployee.getID();
			String wUserName = wBMSEmployee.getName();

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			BMSWorkType wBMSTeam = CloneTool.Clone(wParam.get("data"), BMSWorkType.class);

			if (wBMSTeam.ID <= 0) {
				wBMSTeam.CreatorID = wUserID;
				wBMSTeam.CreatorName = wUserName;
				wBMSTeam.CreateTime = Calendar.getInstance();
			}
			wBMSTeam.EditorID = wUserID;
			wBMSTeam.EditorName = wUserName;
			wBMSTeam.EditTime = Calendar.getInstance();

			ServiceResult<Integer> wServerRst = wBMSService.BMS_SaveWorkType(wBMSEmployee, wBMSTeam);

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {

				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBMSTeam);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, wBMSTeam);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/WorkTypeDelete")
	Object WorkTypeDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			BMSWorkType wBMSWorkType = CloneTool.Clone(wParam.get("data"), BMSWorkType.class);

			ServiceResult<Integer> wServerRst = wBMSService.BMS_DeleteWorkType(wBMSEmployee, wBMSWorkType);

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {

				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBMSWorkType);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, wBMSWorkType);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/WorkTypeActive")
	Object WorkTypeActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data") || !wParam.containsKey("Active")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}
			int wActive = StringUtils.parseInt(wParam.get("Active"));
			List<BMSWorkType> wBMSTeamList = CloneTool.CloneArray(wParam.get("data"), BMSWorkType.class);

			ServiceResult<Integer> wServerRst = new ServiceResult<Integer>(0);
			for (BMSWorkType wBMSWorkType : wBMSTeamList) {
				wServerRst = wBMSService.BMS_ActiveWorkType(wBMSEmployee, wBMSWorkType, wActive);
				if (StringUtils.isNotEmpty(wServerRst.getFaultCode())) {
					break;
				}
			}

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

}
